package com.henghe.frame.core.util;

import org.springframework.util.StringUtils;

import java.util.function.Supplier;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/4/27 09:13
 */
public class TaskUtil {


    public static <T> T foreachTask(Supplier<T> task, int times, long millisecond){
        T rs = null;
        for (int i = 0; i < times; i++){
            // 获取函数参数返回值 返回值是null的话 会继续重试 返回值是字符串的话 会中断循环
            rs = task.get();
            if(rs instanceof String){
                if(!StringUtils.isEmpty(rs)){
                    break;
                }
            }else {
                if(rs != null){
                    break;
                }
            }
            if(i != times - 1){
                try {
                    Thread.sleep(millisecond);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return rs;
    }

}
